package com.cym.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.cym.model.Goods;
import com.cym.service.GoodsService;
import com.cym.utils.BaseController;
import com.cym.utils.JsonResult;

@Controller
@RequestMapping("/goods")
public class GoodsController extends BaseController {

	@Autowired
	GoodsService goodsService;

	@RequestMapping("")
	public ModelAndView goods(ModelAndView modelAndView, String keyword) {
		List<Goods> goodsList = goodsService.search(keyword);
		modelAndView.addObject("keyword", keyword);
		modelAndView.addObject("goodsList", goodsList);
		modelAndView.setViewName("/goods/index");
		return modelAndView;
	}

	@RequestMapping("addOver")
	@ResponseBody
	public JsonResult addOver(Goods goods) {
		jpaHelper.insertOrUpdate(goods);

		return renderSuccess();
	}

	/**
	 * 详情
	 */
	@RequestMapping("detail")
	public ModelAndView see(ModelAndView modelAndView, String id) throws Exception {
		Goods goods = jpaHelper.findById(id, Goods.class);

		modelAndView.addObject("goods", goods);
		modelAndView.setViewName("/goods/detail");
		return modelAndView;
	}

}
